let mixin = {
    data() {
        return {
            screenWidth: document.documentElement.clientWidth || 0,
            screenHeight: document.documentElement.clientHeight,
            loaddingImg: "",
            isMobile: window.navigator.platform == 'iPhone',
        }
    },
    methods: {
        go(url) {
            this.$router.push(url)
        },
        back(url) {
            this.$router.back()
        },
        //返回首页
        toHome() {
            this.$router.goBack("/baseHome")
        },
        getUrlParam(name) {
            let reg = new RegExp('(^|&)' + name + '=([^&]*)(&|$)');
            let result = window.location.search.substr(1).match(reg);
            return result ? decodeURIComponent(result[2]) : null;
        },
        getUrlQuery(url, name) {
            let search = url.split('?')[1] || '';
            let tempArr = search.split('&');
            let tempObj = {};
            tempArr.forEach(val => {
                let valArr = val.split('=');
                tempObj[valArr[0]] = valArr[1];
            });
            return tempObj;
        },
        jump(routerName, param) {
            if (param) {
                this.$router.push({name: routerName, query: param});
            } else {
                this.$router.push({name: routerName});
            }
        },
        jumpReplace(routerName, param) {
            if (param) {
                this.$router.replace({name: routerName, query: param});
            } else {
                this.$router.replace({name: routerName});
            }
        },
        goBack() {
            this.$router.go(-1);
        },
        goHome() {
            this.jump('mymoo');
        },
        // 字段的验证，支持非空、手机、邮箱的判断
        validate(value, type) {
            // 非空验证
            if (type === 'require') {
                return !!value;
            }
            // 手机号验证
            if (type === 'phone') {
                return /^1\d{10}$/.test(value);
            }
            // 邮箱格式验证
            if (type === 'email') {
                return /^(\w)+(\.\w+)*@(\w)+((\.\w{2,3}){1,3})$/.test(value);
            }
            if (type === 'pwd') {
                return /^(?![0-9]+$)(?![a-zA-Z]+$)[0-9A-Za-z]{6,14}$/.test(value);
            }
            if (type === 'number') {
                return /^[0-9]*$/.test(value);
            }
        },
        // 判断是否是微信浏览器
        isWxBrower() {
            let agent = navigator.userAgent.toLowerCase();
            if (agent.match(/MicroMessenger/i) == "micromessenger") {
                return true;
            } else {
                return false;
            }
        },
        // 去除空格
        hcTrim(str) {
            return str.replace(/(^\s*)|(\s*$)/g, "");
        },
        // 去除所有空格
        hcTrimAll(str) {
            return str.replace(/\s+/g,"")
        },
        isNullObject(obj) {
            return JSON.stringify(obj) === '{}' ? true : false
        },
        
        operationMul(arg1, arg2) {
            var m=0,s1=arg1.toString(),
            s2 = arg2.toString();
            try{ m+=s1.split(".")[1].length }catch(e){};
            try{ m+=s2.split(".")[1].length }catch(e){};
            return Number(s1.replace(".", ""))*Number(s2.replace(".", ""))/Math.pow(10, m);
        },
        //时间戳转换
        timeFilter(time, format) {
            var o = {
                'M+': time.getMonth() + 1,
                'd+': time.getDate(),
                'H+': time.getHours(),
                'm+': time.getMinutes(),
                's+': time.getSeconds(),
                'q+': Math.floor((time.getMonth() + 3) / 3),
                'S': time.getMilliseconds()
            };
            if (/(y+)/.test(format)) {
                format = format.replace(RegExp.$1, (time.getFullYear() + '').substr(4 - RegExp.$1.length));
            }
            for (var k in o) {
                if (new RegExp('(' + k + ')').test(format)) {
                format = format.replace(RegExp.$1, RegExp.$1.length === 1 ? o[k] : ('00' + o[k]).substr(('' + o[k]).length));
                }
            }
            return format;
        },
        // 千位分隔符
        numFormat(num){
            num=num.toString().split(".");  // 分隔小数点
            var arr=num[0].split("").reverse();  // 转换成字符数组并且倒序排列
            var res=[];
            for(var i=0,len=arr.length;i<len;i++){
                if(i%3===0&&i!==0){
                    res.push(",");   // 添加分隔符
                }
                res.push(arr[i]);
            }
            res.reverse(); // 再次倒序成为正确的顺序
            if(num[1]){  // 如果有小数的话添加小数部分
                res=res.join("").concat("."+num[1]);
            }else{
                res=res.join("");
            }
            return res;
        },
        // 格式化金额
        amountFormat(number, decimals=2, dec_point, thousands_sep) {
            /*
            * 参数说明：
            * number：要格式化的数字
            * decimals：保留几位小数
            * dec_point：小数点符号
            * thousands_sep：千分位符号
            * */
            number = (number + '').replace(/[^0-9+-Ee.]/g, '');
            var n = !isFinite(+number) ? 0 : +number,
            prec = !isFinite(+decimals) ? 0 : Math.abs(decimals),
            sep = (typeof thousands_sep === 'undefined') ? ',' : thousands_sep,
            dec = (typeof dec_point === 'undefined') ? '.' : dec_point,
            s = '',
            toFixedFix = (n, prec) => {
                var k = Math.pow(10, prec);
                return '' + Math.ceil(this.operationMul(n, k)) / k;
            };
            s = (prec ? toFixedFix(n, prec) : '' + Math.round(n)).split('.');
            var re = /(-?\d+)(\d{3})/;
            while (re.test(s[0])) {
                s[0] = s[0].replace(re, "$1" + sep + "$2");
            };
            if ((s[1] || '').length < prec) {
                s[1] = s[1] || '';
                s[1] += new Array(prec - s[1].length + 1).join('0');
            };
            return s.join(dec);
        },
        // 合并对象
        objExtend() {
            let arg = arguments;
            let target = arg[0];
            let tmpObj = null;
            for (let i = 1; i < arg.length; i++) {
                tmpObj = arg[i];
                for (let key in tmpObj) {
                    if (tmpObj.hasOwnProperty(key)) {
                        target[key] = tmpObj[key];
                    }
                }
            }
        },
        clearAllCookie() {
            let keys = document.cookie.match(/[^ =;]+(?=\=)/g);
            if(keys) {
                for(let i = keys.length; i--;) {
                console.log(i)
                document.cookie = keys[i] + '=0;expires=' + new Date(0).toUTCString()
                }
            }
        },
        scrollToTopForBug() {
            window.scrollTo(0,0);
        },
        bodyStyleGetFixed() {
            let bodyDom = document.getElementsByTagName("body")[0];
            if(bodyDom) {
                bodyDom.style.position = 'fixed';
            }
        },
        bodyStyleRemoveFixed() {
            let bodyDom = document.getElementsByTagName("body")[0];
            if(bodyDom) {
                bodyDom.style.position = '';
            }
        }
    }
};

export default mixin;
